import { Component, Emit, Prop, Watch } from 'vue-facing-decorator'
import Render from '@/render/Render'
import toNative from '@/utils/toNative'
import {
    getModelValue,
    initModelValue,
    renderForm,
    type FormModalDto
} from '@/components/modal/Modal'
import { getKeyToEnum, InsureTypeCnEnum } from '@/const/enum'
import type { AddCommunicationConfigReqDto, ConfigListDto } from '@/api/emailConfig/types'

type ValueType = Pick<AddCommunicationConfigReqDto, 'ruleName' | 'insuranceType'>

@Component({
    emits: ['submit', 'initData']
})
export class BasicRule extends Render {
    basicFormConfig = this.initBasicFormConfig() as FormModalDto
    initBasicFormConfig() {
        return {
            title: '',
            width: '400px',
            theme: 'light',
            show: false,
            items: {
                ruleName: {
                    name: '规则名称',
                    element: 'input',
                    required: true
                },
                insuranceType: {
                    name: '保险产品',
                    element: 'radioGroup',
                    options: getKeyToEnum(InsureTypeCnEnum, false, ['长期保']),
                    defaultValue: InsureTypeCnEnum.长期保,
                    required: true
                }
            },
            complete: async () => {},
            cancel: () => {
                this.basicFormConfig.show = false
            },
            init: () => {
                initModelValue(this.basicFormConfig)
            }
        } as FormModalDto
    }

    basicForm = {
        validate: async () => {},
        clear: () => {},
        element: () => <> </>
    }

    initData(data: ConfigListDto) {
        initModelValue(this.basicFormConfig, data)
    }

    async submit() {
        await this.basicForm.validate()
        const data = getModelValue(this.basicFormConfig)
        return data
    }

    onMounted() {
        this.basicForm = renderForm(this.basicFormConfig)
    }

    render() {
        return <>{this.basicForm.element()}</>
    }
}

export default toNative<{}, {}>(BasicRule)
